const path = require('path')
// const WebpackObfuscator = require('webpack-obfuscator');

class ChineseToUnicodePlugin {
  apply(compiler) {
    compiler.hooks.emit.tapAsync(
      'ChineseToUnicodePlugin',
      (compilation, callback) => {
        for (const assetName in compilation.assets) {
          let content = compilation.assets[assetName].source()

          // 将中文字符转换为 Unicode
          content = content.replace(/[\u4e00-\u9fa5]/g, function(char) {
            return '\\u' + char.charCodeAt(0).toString(16)
          })

          // 更新资源内容
          compilation.assets[assetName] = {
            source: () => content,
            size: () => content.length
          }
        }
        callback()
      }
    )
  }
}

module.exports = {
  lintOnBuild: false,
  postHook(config) {
    config.resolve.alias = {
      '@': path.resolve(__dirname, 'src')
    }

    config.plugins.push(
      new (require('webpack-obfuscator'))({
        rotateUnicodeArray: true,
        exclude: /node_modules/
      })
    )

    config.plugins.push(new ChineseToUnicodePlugin())
  }
}
